var sex = 0;//性别
var marry = 1;//婚否
var uid,ws,page,photo;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	page = ws.page;
	console.log(page);
	uid = plus.storage.getItem('uid');//用户id
	$("input[name='radio1']").click(function(){
    	sex= $(this).attr("value");
    });
    $("input[name='radio2']").click(function(){
    	marry= $(this).attr("value");
    });
	//添加病例
	$('.bingli').click(function(){
		goNewPage('me-archives-home-add-case.html',{spage:ws.id})
	})
	//自我检测
	$('.ziwotest').click(function(){
		goNewPage('tzjc-home-list.html',{spage:ws.id});
	})
//  console.log(uid);
    //上传照片-》单图
	$('.uploadPic').on('tap', function() {
		mui('#sheet2').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao2').on('tap', function(){
		var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			path = plus.io.convertLocalFileSystemURL(path);
			$('.uploadPic').attr('src',path);
			appendonePic('file://'+path);
		}, function(err){}, {index:1} );
	})
	//选择相册添加
	$('.photo2').on('tap', function(){
		plus.gallery.pick( function(path){
		 	$('.uploadPic').attr('src',path);
		 	appendonePic(path);
		}, function(error){}, {} );
	})
    var index = 1;
	var files = [];
	// 添加照片
	function appendonePic(p) {
		files.push({name:"uploadkey"+index,path:p});
		index++;
		upload();
	}
	//创建对像
	function upload(){ 
		showWating();
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadOne',
		{ method:"POST"},
		function ( t, status ) {  
			// 上传完成     
			if ( status == 200 ) { 
				console.log(t.responseText);
				plus.nativeUI.closeWaiting();
				if(t.responseText == -1){
					toast('上传失败');
				}else{
//					plus.nativeUI.toast("上传成功");    
					$(".uploadPic").attr('src',webRoot+t.responseText);
					photo = t.responseText;
				}
			} else {
				plus.nativeUI.toast( "上传失败："+status );
				plus.nativeUI.closeWaiting();
			}  
		});
//		console.log(JSON.stringify(task));
		for(var i=0;i<files.length;i++){
			var f=files[i];
			task.addFile(f.path,{key:f.name});
		}
		task.start();
	}
    //点击完成，添加家庭成员
    $('.addover').click(function(){
    	var name = $('.name').val();
    	var age = $('.age').val();
    	var nation = $('.nation').val();
    	var phone = $('.phone').val();
    	var height = $('.height').val();
    	var weight = $('.weight').val();
    	var address = $('.address').val();
    	var physi = $('.physi').val();
    	var bid = ',';
    	$('.blist').each(function(){
    		bid += $(this).attr('bid') + ",";
    	})
    	console.log(name+" "+age+" "+nation+" "+phone+" "+address+" "+sex+" "+marry+" "+photo+" "+height+" "+weight+" "+bid+" "+physi);
    	if(!name.trim() || !age.match(a) || !nation.trim() || !phone.trim() || !address.trim() || !photo || !height.match(h) || !weight.match(w)){
    		toast('请补全信息并检查是否正确');return;
    	}
    	if(!phone.match(p1)){
    		toast('手机号格式错误');
    	}
    	if(bid == ',')bid='';
    	showWating();
		$.ajax({
			type:"get",
//			url:apiRoot+'?m=Home&c=member&a=addFamily&uid='+uid+'&name='+name+'&age='+age+'&nation='+nation+'&phone='+phone+'&address='+address+'&sex='+sex+'&marry='+marry+'&photo='+photo+'&cases='+bid,
			url:apiRoot+'?m=Home&c=member&a=addFamily',
			data : {
				uid :uid,
				name :name,
				age :age,
				nation :nation,
				phone :phone,
				address : address,
				sex :sex,
				marry :marry,
				height :height,
				weight :weight,
				photo :photo,
				cases :bid,
				physical : physi
			},
			success:function(data){
				console.log(data);
				plus.nativeUI.closeWaiting();
				if(data > 1){
					if(page)plus.webview.getWebviewById(page).reload();
					setTimeout(function(){ 
						plus.webview.currentWebview().close();
					} , 2000); 
				}else{
					toast('保存失败');
				}
			},
			error:function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
    })
},false)
window.addEventListener('change',function(event){
	var bid = event.detail.bid;
	var aidsname = event.detail.aidsname;
	var ztime = event.detail.ztime;   
	var bing = event.detail.bing;
	console.log(bid+"," +aidsname+', '+ztime+', '+bing);
	$('.wubing').hide();
	$('.bingl').show();
	var list = '<div class="blist" bid="'+bid+'"><li><span>'+aidsname+'</span><span class="mui-h6 mui-pull-right">'+ztime+'</span></li><li class="font-12 line-h-25">'+bing+'</li></div>';
	$('.bingl').append(list);
})
window.addEventListener('ziwotest',function(event){
	var physical = event.detail.physical;
	console.log(physical);
	$('.physi').val(physical);
	console.log($('.physi').val());
})